#import <TZImagePickerController/TZImagePickerController.h>

#import "MYRegisterSetPerfectInfoVC.h"
#import "BRPickerView.h"

#import "SDDatePickerView.h"

@interface MYRegisterSetPerfectInfoVC ()<MYRegisterSetPerfectInfoSelectCellDelegate,TZImagePickerControllerDelegate,SDDatePickerViewDelegate>
@property (nonatomic, copy) NSString * sexStr;
@property (nonatomic, copy) NSString * dateStr;

@property (nonatomic, weak) TZImagePickerController *imagePicker;
@property (nonatomic, strong) UIImage *photo;

@property (strong, nonatomic) SDDatePickerView *dateView;

@end

@implementation MYRegisterSetPerfectInfoVC

- (SDDatePickerView *)dateView {
    if (!_dateView) {
        SDDatePickerView *dateView = [[SDDatePickerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 44 + 199)];
        dateView.delegate = self;
        dateView.isSlide = NO;
        [self.view addSubview:dateView];
        _dateView = dateView;
    }
    return _dateView;
}

#pragma mark - SDDatePickerViewDelegate
- (void)datePickerViewSaveBtnClickDelegate:(NSString *)timer
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];

    MYRegisterSetPerfectInfoSelectCell *cell = (MYRegisterSetPerfectInfoSelectCell *)[self.listTableView cellForRowAtIndexPath:indexPath];
    
    BOOL isCommon = [YQPInsureValidate checkDateBeginDate:timer];
    if (isCommon) {
        [MBProgressHUD showSuccess:kYQLS(@"选择日期不得超过当前日期")];
        cell.textField.text = [[YQPInsureValidate getCurrentDate] substringToIndex:10];
    }else{
        cell.textField.text = timer;
    }
    self.dateStr = cell.textField.text;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rightItemTitle = kYQLS(@"跳过");
    [self.rightItem setTitleColor:HexColor(0x79C4C7) forState:(UIControlStateNormal)];
    
}

-(void)rightItemAction:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:true];
}

-(void)setupView {
    [super setupView];
    [self.listTableView registerNib:[UINib nibWithNibName:@"MYAccountsCommonHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"MYAccountsCommonHeaderView"];
    [self.listTableView registerClass:[MYRegisterSetPerfectInfoSelectCell class] forCellReuseIdentifier:@"MYRegisterSetPerfectInfoSelectCell"];
    [self.listTableView registerClass:[MYRegisterFindPasswordTextFieldCell class] forCellReuseIdentifier:@"MYRegisterFindPasswordTextFieldCell"];
    [self.listTableView registerClass:[MYLoginBaseBtnCell class] forCellReuseIdentifier:@"MYLoginBaseBtnCell"];

}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MYRegisterFindPasswordTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYRegisterFindPasswordTextFieldCell"];
        cell.textField.textColor = HexColor(0x414141);
        cell.placeholderStr = kYQLS(@"请输入你的姓名");
        return cell;
    } else if(indexPath.row == 1 || indexPath.row == 2){
        MYRegisterSetPerfectInfoSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYRegisterSetPerfectInfoSelectCell"];
        cell.indexPath = indexPath;
        cell.delegate = self;
        cell.textField.textColor = HexColor(0x414141);
        if (indexPath.row == 1) {
            cell.placeholderStr = kYQLS(@"请选择你的性别");
        } else {
            cell.placeholderStr = kYQLS(@"请选择你的出生日期");
        }
        return cell;
    }else{
        MYLoginBaseBtnCell *loginCell = [tableView dequeueReusableCellWithIdentifier:@"MYLoginBaseBtnCell"];
        [loginCell.loginBtn setBackgroundColor:HexColor(0x79C4C7)];
        [loginCell.loginBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
        [loginCell.loginBtn addTarget:self action:@selector(doneAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [loginCell.loginBtn setTitle:kYQLS(@"完成") forState:(UIControlStateNormal)];
        return loginCell;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 3) {
        return 100;
    } else {
        return 56;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
//        [self presentViewController:self.imagePicker animated:YES completion:nil];
//
//    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MYAccountsCommonHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYAccountsCommonHeaderView"];
    headerView.titleLb.text = kYQLS(@"完善个人信息");
    headerView.subheadLb.text = kYQLS(@"继续完成个人信息的填写");
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 115;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
-(void)doneAction:(UIButton *)sender {
    [self updateUserNameApi];
}

#pragma makr -- MYRegisterSetPerfectInfoSelectCellDelegate
- (void)gotoSelectAction:(NSIndexPath *)indexPath {
    MYRegisterSetPerfectInfoSelectCell *cell = (MYRegisterSetPerfectInfoSelectCell *)[self.listTableView cellForRowAtIndexPath:indexPath];
    
    if (indexPath.row == 1) {
        [self.view endEditing:true];
        [BRStringPickerView showStringPickerWithTitle:@"" dataSource:@[kYQLS(@"女"),kYQLS(@"男")] defaultSelValue:kYQLS(@"男") isAutoSelect:NO themeColor:kAPPThemeColor resultBlock:^(id selectValue) {
            self.sexStr = selectValue;
            cell.textField.text = self.sexStr;
        } cancelBlock:^{
            
        }];
    }else if (indexPath.row == 2) {
//        NSDate *minDate = [NSDate br_setYear:1900 month:1];
//        [BRDatePickerView showDatePickerWithTitle:@"" dateType:(BRDatePickerModeYM) defaultSelValue:@"1990-01-01" minDate:minDate maxDate:[NSDate date] isAutoSelect:NO themeColor:kAPPThemeColor resultBlock:^(NSString *selectValue) {
//            self.dateStr = selectValue;
//            cell.textField.text = self.dateStr;
//        } cancelBlock:^{
//
//        }];
        [self.dateView show];

    }
}

#pragma mark - 设置用户名
- (void)updateUserNameApi
{
    MYRegisterSetPerfectInfoSelectCell *cell = (MYRegisterSetPerfectInfoSelectCell *)[self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    if (cell.textField.text.length == 0) {
        [QMUITips showInfo:kYQLS(@"请输入姓名") inView:self.view];
        return;
    }
    if (self.dateStr.length == 0) {
        [QMUITips showInfo:kYQLS(@"请选择日期") inView:self.view];
        return;
    }
    
    NSString *sex;
    if ([self.sexStr isEqualToString:kYQLS(@"女")]) {
        sex = @"0";
    }else{
        sex = @"1";

    }
    if (sex.length == 0) {
        [QMUITips showInfo:kYQLS(@"请选择性别") inView:self.view];
        return;
    }
    NSMutableDictionary *dic = NSMutableDictionary.new;
    dic[@"userId"] = self.userId;
    dic[@"username"] = self.username;
    dic[@"birthday"] = self.dateStr;
    dic[@"gender"] = @(sex.intValue);
    dic[@"headImg"] = self.username;
    dic[@"name"] = cell.textField.text;

    [YQPNetWorkingManager sendPostRequest:kPOST_COMPLETEINFORMATION_URL parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [MBProgressHUD showSuccess:responseObject.msg];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
}
#pragma mark - TZImagePickerControllerDelegate

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    picker.allowCrop = YES;
    UIImage *photo = photos[0];
    _photo = photo;

}

- (TZImagePickerController *)imagePicker
{
    if (self.imagePicker == nil) {
        TZImagePickerController *imagePicker = [[TZImagePickerController alloc] initWithMaxImagesCount:4 delegate:self];
        [imagePicker setNaviBgColor:kAPPThemeColor];
        [imagePicker setNaviTitleColor:UIColor.whiteColor];
        [imagePicker setIconThemeColor:kAPPThemeColor];
        [imagePicker setOKButtonTitleColorNormal:kAPPThemeColor];
        [imagePicker setOKButtonTitleColorDisabled:kAPPLineViewColor];
        [imagePicker.view setNeedsDisplay];
        imagePicker.maxImagesCount = 1;
        self.imagePicker = imagePicker;
    }
    return self.imagePicker;
}
@end
